<?php
/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 7/18/2016
 * Des: 系统管理员
 */
defined('INHkMvc') or exit('Access Invalid!');

class App_task_goodModel extends AppModel
{

    private $task_num = 20;
    private $phpize = "/usr/local/php/bin/php" . SPACE . "-f" . SPACE . '/usr/local/apache2/htdocs/dekahome/';
    private $per = 20;

    /**
     * 添加任务
     * @param $query
     * @param $at_time
     * @param int $cpu
     * @return bool
     */
    public function add($query, $at_time, $priority = 0, $cpu = 20)
    {
        if ($query == "") {
            return false;
        }
        $data = [];
        $data["query"] = "cpulimit -l" . SPACE . $cpu . SPACE . trim($query);
        $data["add_time"] = time();
        $data["at_time"] = $at_time;
        $data["cpu"] = $cpu;
        $data["priority"] = $priority;
        return $this->insert($data);
    }

    /**
     * 获取任务
     * @return array
     */
    public function getTask()
    {
        $condition = [];
        $condition[] = ["status" => ['gt', 1, 'AND']];
        $data = $this->limit($this->task_num)->order("status desc, priority desc, id desc, add_time desc")->select($condition);
        return $data;
    }

    /**
     * 更新产品
     * @return bool
     */
    public function updateGood()
    {
        $per = 10;
        $count = M("app.member_vip")->getVipNum();
        $at_time = strtotime(date("Y-m-d 03:00:00", time()));
        $this->begin();
        for ($i = 1; $i <= ceil($count / $per); $i++) {
            if ($this->add($this->phpize . "index.php ssh updateGood" . SPACE . $i . SPACE . $per, $at_time) === false) {
                $this->log->debug_write($this->phpize . SPACE . "-f" . SPACE . "index.php ssh updateGood" . SPACE . $i . SPACE . $per);
                $this->rollback();
                return false;
            }
        }
        $this->commit();
        return true;
    }

    public function updateGoodForMemberVip($id, $level)
    {
        $at_time = time() - 120;
        $task_id = $this->add($this->phpize . "index.php ssh reviewedMemberVip" . SPACE . $id . SPACE . $level, $at_time, 1);
        if ($task_id === false) {
            return false;
        } else {
            return $task_id;
        }
    }

    public function updateGoodStatus(){
        $at_time = time() - 120;
        $task_id = $this->add('/usr/local/php/bin/php -f /usr/local/apache2/htdocs/crm/index.php api updateGoodStatus', $at_time, 1);
        if ($task_id === false) {
            return false;
        } else {
            return $task_id;
        }
    }

    /**
     * 更新pid
     * @param $id
     * @param $pid
     * @return bool
     */
    public function updatePid($id, $pid)
    {
        $res = $this->update(['pid' => $pid, 'status' => 3, 'start_time' => time()], ['id' => $id]);
        if ($res === false) {
            $this->log->debug_write($this->table . ":" . $id . " update pid failed");
            return false;
        }
        return true;
    }

    /**
     * 获取线程数
     * @return int
     */
    public function getPer()
    {
        return $this->per;
    }

    /**
     * 设置线程数
     * @param $per
     */
    public function setPer($per)
    {
        $this->per = $per;
    }

    /**
     * 更新状态
     * @param $id
     * @param int $status
     * @return bool
     */
    public function updateStatus($id, $status = 1)
    {
        $res = $this->update(['status' => $status, 'end_time' => time()], ['id' => $id]);
        if ($res === false) {
            $this->log->debug_write($this->table . ":" . $id . " update status failed");
            return false;
        }
        return true;
    }

    public function updateGoodPlan($id)
    {
        $at_time = time() - 120;
        $task_id = $this->add('/usr/local/php/bin/php -f /usr/local/apache2/htdocs/crm/index.php api updatePlanById' . SPACE . $id, $at_time, 1);
        if ($task_id === false) {
            return false;
        } else {
            return $task_id;
        }
    }
}
